#include "plwordnetdao.h"

#include <QSqlQuery>
#include <QSqlError>

#include <QDebug>

PlWordNetDAO::PlWordNetDAO()
{
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("plwordnet_20");
    db.setUserName("root");
    db.setPassword("password");
    if (!db.open())
        qCritical("Database Error: ", qPrintable(db.lastError().text()));
}

QStringList PlWordNetDAO::findSynonyms(const QString &lemma) const
{
    QSet<QString> synonyms;
    QSqlQuery synsetQuery(QString("select syn_id from plwordnet_20.unitandsynset join plwordnet_20.lexicalunit "
                              "on lex_id = id "
                              "where lemma = \"%1\";"
                     ).arg(lemma));
    synsetQuery.setForwardOnly(true);
    while(synsetQuery.next()){
        qlonglong synset = synsetQuery.value(0).toLongLong();
        QSqlQuery synonymQuery(QString("select lemma from plwordnet_20.unitandsynset join plwordnet_20.lexicalunit "
                                       "on lex_id = id "
                                       "where syn_id = %1;"
                              ).arg(synset));
        synonymQuery.setForwardOnly(true);
        while(synonymQuery.next())
            synonyms << synonymQuery.value(0).toString();
    }
    return synonyms.toList();
}
